package com.zzl.zzloj.judge.codesandbox.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.zzl.zzloj.common.ErrorCode;
import com.zzl.zzloj.exception.BusinessException;
import com.zzl.zzloj.judge.codesandbox.CodeSandbox;
import com.zzl.zzloj.judge.codesandbox.model.ExcuteCodeRequest;
import com.zzl.zzloj.judge.codesandbox.model.ExcuteCodeResponse;
import org.apache.commons.lang3.StringUtils;

public class RemoteCodeSandbox implements CodeSandbox {
    @Override
    public ExcuteCodeResponse excuteCode(ExcuteCodeRequest excuteCodeRequest) {
        String url = "http://localhost:8090/executeCode";
        String requestStr = JSONUtil.toJsonStr(excuteCodeRequest);
        String responseStr = HttpUtil.createPost(url)
                .body(requestStr)
                .execute()
                .body();
        if(StringUtils.isBlank(responseStr)){
            throw new BusinessException(ErrorCode.API_REQUEST_ERROR,"执行remoteSandbox出错");
        }
        return JSONUtil.toBean(responseStr,ExcuteCodeResponse.class);
    }
}
